<?php
	/**
	*Author 雷日锦 362606856@qq.com 
	*控制器自动生成
	*/
	if(!defined("ROOT_PATH")) exit("die Access ");
	class ymdianControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$id=get('id','i');
			if($id){
				setcookie("ckymdian",$id,time()+36000000,"/",DOMAIN);
				 
			}elseif(isset($_COOKIE['ckymdian'])){
				$id=intval($_COOKIE['ckymdian']);
			}else{
				$this->goALL("先选择楼栋",123,0,"/index.php?m=ymdian&a=loudong");
			}
			$where=" id=".$id;
			$shop=M("ymdian")->selectRow($where);
			if(empty($shop)){
				setcookie("ckymdian",0,time()-36000000,"/",DOMAIN);
				$this->goALl("店铺不存在",1,0,"/index.php");
			}
			if($shop['status']!=2){
				setcookie("ckymdian",0,time()-36000000,"/",DOMAIN);
				$this->goAll("店铺关闭整顿中..",1,0,"/index.php?m=ymdian&a=loudong");
			}
			
			//M("ymdian_product")->update(array("status"=>2)," shopid=".$shop['id']." AND status=4 AND total_num > 0 ");
			if($shop['siteid']!=SITEID){
				$this->goUrl("/index.php?m=ymdian&a=loudong");
			}
			$option=array(
				"where"=>" 1 ",
			);
			$prolist=M("ymdian_product")->lsitByShopCat($id);
			$cart=M("ymdian_order_cart")->getListByUser(" AND shopid=".$id);
			$total_amount=$total_money=0; 
			if($prolist){
				foreach($prolist as $k=>$p){
					if($p){
						foreach($p as $kk=>$v){
							if(isset($cart['product'][$v['id']])){
								 
								$v['cart_amount']=$cart['product'][$v['id']]['amount'];
								$total_amount+=$v['cart_amount'];
								$total_money+=$v['cart_amount']*$v['price'];
							}else{
								$v['cart_amount']=0;
							}
							$p[$kk]=$v;
						}
					}
					$prolist[$k]=$p;
				}
			}
			$bdata=array(
					"shop"=>$shop,
					"cart"=>$cart,
					"carturl"=>"/index.php?m=ymdian_order_cart&shopid=".$shop['id'],
					"prolist"=>$prolist,
					"catlist"=>M("ymdian_product")->catlist()
			);
			if(get('ajax')){
				$this->goAll("success",0,$bdata);
			}
			$this->smarty->assign(
				$bdata
			);
			$this->smarty->display("ymdian/index.html");
		}
		
		public function onSHow(){
			$this->smarty->display("ymdian/show.html");
		}
		
		
		public function onProduct(){
			$id=get('id','i');
			if($id){
				setcookie("ckymdian",$id,time()+36000000,"/",DOMAIN);		 
			}elseif(isset($_COOKIE['ckymdian'])){
				$id=intval($_COOKIE['ckymdian']);
			}else{
				$this->goAll("参数出错",1);
			}
			$shop=M("ymdian")->selectRow($where);
			$option=array(
				"where"=>" 1 ",
			);
			$prolist=M("ymdian_product")->lsitByShopCat($id);
			$cart=M("ymdian_order_cart")->getListByUser(" AND shopid=".$id);
			 
			if($prolist){
				foreach($prolist as $k=>$p){
					if($p){
						foreach($p as $kk=>$v){
							if(isset($cart['product'][$v['id']])){
								 
								$v['cart_amount']=$cart['product'][$v['id']]['amount'];
							}else{
								$v['cart_amount']=0;
							}
							$p[$kk]=$v;
						}
					}
					$prolist[$k]=$p;
				}
			}
			$this->goALL("success",0,$prolist);
		}
		
		public function onloudong(){
			$data=M('site_city')->children(0,1);
			$ymids=M('ymdian')->getScShopIds(SITEID);
			
			if($data){
				foreach($data as $k=>$v){
					if(isset($ymids[$v['sc_id']])){
						$v['shopid']=$ymids[$v['sc_id']]['id'];
					}
					if($v['child']){
						foreach($v['child'] as $kk=>$vv){
							if(isset($ymids[$vv['sc_id']])){
								
								$vv['shopid']=$ymids[$vv['sc_id']]['id'];
								$vv['opentype']=shopopen($ymids[$vv['sc_id']]['yystart'],$ymids[$vv['sc_id']]['yyend']);
								if($ymids[$vv['sc_id']]['status']!=2){
									$vv['opentype']="close";
								}
							}
							$v['child'][$kk]=$vv;
						}
					}
					$data[$k]=$v;
				}
			}
			$ndata=array(
				"data"=>$data,
				"ymids"=>$ymids
			);
			if(get_post('ajax')){
				$this->goAll("success",0,$ndata);
			}
			$this->smarty->assign($ndata);
			$this->smarty->display("ymdian/loudong.html");
		}
		
	}
 
?>